using System;
using System.Reflection;
using System.Web.UI.WebControls;

using Impact.Controllers;
using Impact.Views;
using Impact.Web.Foundation;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.Security;

namespace Impact.Web.Install
{
    public partial class Default : PageBase, IInstallerView
    {
        private InstallController controller;

        /// <summary>
        /// instantiate and initializes the controller
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {

            base.OnLoad(e);

            controller = new InstallController(this);
            controller.Initialize(!IsPostBack);

        }

        /// <summary>
        /// event handler executed by any button on the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FormCommand(object sender, CommandEventArgs e)
        {

            if (IsValid)
            {
                Type controllerType = controller.GetType();
                MethodInfo method = controllerType.GetMethod(e.CommandName);
                if (method != null)
                {
                    method.Invoke(controller, null);
                }
                else
                {
                    throw new ArgumentException(
                        string.Format("Type '{0}' does not have a method named '{1}'", controllerType.ToString(),
                                      e.CommandName));
                }
            }
        }

        #region IInstallerView Members

        /// <summary>
        /// retrieve the number of the current step out of the state machine
        /// </summary>
        public int CurrentStep
        {
            get { return (int)ViewState["step"]; }
            set { ViewState["step"] = value; }
        }

        /// <summary>
        /// run validation controls and return true if all pass
        /// </summary>
        public new bool IsValid
        {
            get
            {
                Validate(string.Format("Step{0}", CurrentStep));
                return base.IsValid;
            }
        }

        /// <summary>
        /// display the given step in the view
        /// </summary>
        public void ShowStep(int stepNumber)
        {
            Step1.Visible = false;
            Step2.Visible = false;
            Step3.Visible = false;
            StepFinish.Visible = false;

            switch(stepNumber)
            {
                case 1:
                    Step1.Visible = true;
                    break;
                case 2:
                    Step2.Visible = true;
                    break;
                case 3:
                    Step3.Visible = true;
                    break;
                case 99:
                    StepFinish.Visible = true;
                    break;
                default:
                    throw new ArgumentException(string.Format("Step {0} does not exist.", stepNumber));
            }
        }

        /// <summary>
        /// retrieve the data to be used for the administrator
        /// </summary>
        public AccountData Administrator
        {
            get
            {
                AccountData newAccount = new AccountData();
                newAccount.Username = username.Text;
                newAccount.Password = password.Text;
                newAccount.Email = email.Text;
                return newAccount;
            }
        }

        #endregion
    }
}
